home *** CD-ROM | disk | FTP | other *** search
- { This unit implements routines to access and manipulate the DOS }
- { environment variable PATH and to use the PATH to find a user }
- { specified file. I thought I had seen routines to do this }
- { somewhere but couldn't find them when I went looking. I hope }
- { that others will find them useful. This is a first release and }
- { comments are welcome. Address them to 70441,3321 on BPROGA or }
- { via Easyplex. }
- { }
- { }
- { FindFirstPath and FindNextPath are modeled on TP's FindFirst }
- { and FindNext file finding routines. They use a PathRec in place }
- { of a SearchRec and the test condition is PathRec.CurDir = '' }
- { rather than DOSError <> 0 (See the source of FileFind, below). }
- { }
- { FindFile uses FindFirstPath and FindNextPath to find a user }
- { specified file in the current directory or anywhere on the user's }
- { PATH. If the file is not fount, FindFile returns a null string. }
- { }
- { Chris Nelson }
- { 70441,3321 }
- { 900209 }
- UNIT FindPath;
-
- INTERFACE
-
- USES DOS;
-
- TYPE
- PathRec = Record
- EnvPath : String;
- CurDir : PathStr;
- Pt : INTEGER;
- END;
-
- PROCEDURE FindFirstPath(VAR PathIn : PathRec);
- PROCEDURE FindNextPath(VAR PathIn : PathRec);
- FUNCTION FindFile(FileName : PathStr) : PathStr;
- { $EJECT}
- {--------------------------------------------------------------------}
- { }
- IMPLEMENTATION
-
- FUNCTION FileExists(FileName : PathStr) : BOOLEAN;
-
- VAR
- TestFile : FILE OF BYTE;
-
- BEGIN
- Assign(TestFile,FileName);
- {$I-}
- RESET(TestFile);
- {$I+}
- FileExists := (IORESULT = 0);
- END;
- { $EJECT}
- {--------------------------------------------------------------------}
- { }
- PROCEDURE FindFirstPath(VAR PathIn : PathRec);
-
- BEGIN WITH PathIn DO BEGIN
- EnvPath := GetEnv('Path');
- IF LENGTH(EnvPath) = 0 THEN BEGIN
- CurDir := FExpand('.\');
- END
- ELSE BEGIN
- Pt := POS(';',EnvPath);
- IF Pt > 0 THEN BEGIN
- CurDir := FExpand(COPY(EnvPath, 1, Pt - 1));
- EnvPath := COPY(EnvPath,Pt + 1,Length(EnvPath));
- END
- ELSE BEGIN
- CurDir := FExpand(EnvPath);
- EnvPath := '';
- END
- END;
-
- IF CurDir[Length(CurDir)] <> '\' THEN
- CurDir := CONCAT(CurDir,'\');
-
- END; {WITH}
- END; {PROCEDURE FindFirstPath}
- { }
- {--------------------------------------------------------------------}
- { }
- PROCEDURE FindNextPath(VAR PathIn : PathRec);
-
- BEGIN WITH PathIn DO BEGIN
- IF Length(EnvPath) = 0 THEN BEGIN
- CurDir := '';
- EXIT;
- END;
-
- Pt := POS(';',EnvPath);
- IF Pt > 0 THEN BEGIN
- CurDir := FExpand(COPY(EnvPath, 1, Pt - 1));
- EnvPath := COPY(EnvPath,Pt + 1,Length(EnvPath));
- END
- ELSE BEGIN
- CurDir := FExpand(EnvPath);
- EnvPath := '';
- END;
-
- IF CurDir[Length(CurDir)] <> '\' THEN
- CurDir := CONCAT(CurDir,'\');
-
- END; {WITH}
- END; {PROCEDURE FindNextPath}
- { $EJECT}
- {--------------------------------------------------------------------}
- { }
- FUNCTION FindFile(FileName : PathStr) : PathStr;
-
- VAR
- PathInfo : PathRec;
-
- BEGIN
- IF FileExists(CONCAT(FExpand('.\'),FileName)) THEN BEGIN
- FindFile := CONCAT(FExpand('.\'),FileName);
- EXIT;
- END;
-
- FindFirstPath(PathInfo);
- WHILE PathInfo.CurDir <> '' DO BEGIN
- IF FileExists(CONCAT(PathInfo.CurDir,FileName)) THEN BEGIN
- FindFile := CONCAT(PathInfo.CurDir,FileName);
- EXIT;
- END
- ELSE
- FindNextPath(PathInfo);
- END;{WHILE}
- END; {FUNCTION FindFile}
-
- END. {UNIT FindPath}